//
//  ApplyOilSecondVC.m
//  Base_App
//
//  Created by Yutian Duan on 2019/6/14.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "ApplyOilSecondVC.h"
#import "ApplyOilThirdVC.h"

#import "ApplyeOilHeadView.h"
#import "OilCradCreateTableView.h"
#import "OilCradCreateCell.h"
#import "OilCradCreateModel.h"

@interface ApplyOilSecondVC () <UITableViewDataSource,UITableViewDelegate> {
  NSMutableArray *array;
}

@property (nonatomic, strong) ApplyeOilHeadView *stepHeadView;
@property (nonatomic, strong) UIScrollView *contenView;
@property (nonatomic, strong) OilCradCreateTableView *tableView;
@end

@implementation ApplyOilSecondVC

- (void)viewDidLoad {
  [super viewDidLoad];
  
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  //  [self setDefaltNavBar];
  [self.navigationController setNavigationBarHidden:NO animated:animated];
  [self setNavImage:[UIImage imageNamed:@"Home_BgImage"] titleColor:[UIColor whiteColor]];
  [self setStatusBarStyleWhite];
  
}


- (void)createFields {
  [super createFields];
  self.navigationItem.title = YTLocalizedString(@"油咭申请");
  [self createWhiteBack];
  
  
  array = [[NSMutableArray alloc] init];
  [array addObject:[[OilCradCreateModel alloc] init]];
}

- (void)createViews {
  [super createViews];
  
//  _contenView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight)];
//  [self.view addSubview:_contenView];
  
  _tableView = [[OilCradCreateTableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight - 50) style:UITableViewStylePlain];
  _tableView.delegate = self;
  _tableView.dataSource = self;
  [self.view addSubview:_tableView];
  
  
  
  _stepHeadView = [[ApplyeOilHeadView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * ITEMPRO)];
  _stepHeadView.stepCount = 1;
  _stepHeadView.userInteractionEnabled = NO;
  _stepHeadView.isPersonal = _applyModel.isPersonal;
  _tableView.tableHeaderView = _stepHeadView;
  
  [self createApplyView];
  
  ///! 取消
  UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, _tableView.yt_bottom + 10, 30 * 5, 30)];
  [cancelButton setTitle:YTLocalizedString(@"上一步") forState:UIControlStateNormal];
  cancelButton.backgroundColor = DEF_GRAY;
  cancelButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
  [cancelButton addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
  cancelButton.yt_right = self.view.yt_centerX - 5;
  cancelButton.layer.masksToBounds = YES;
  cancelButton.layer.cornerRadius = 8;
  [self.view addSubview:cancelButton];
  
  ///! 下一步
  UIButton *nextStepButton = [[UIButton alloc] initWithFrame:CGRectMake(cancelButton.yt_right + 10, cancelButton.yt_top, cancelButton.yt_width, cancelButton.yt_height)];
  [nextStepButton setTitle:YTLocalizedString(@"下一步") forState:UIControlStateNormal];
  nextStepButton.backgroundColor = DEF_RED;
  nextStepButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
  [nextStepButton addTarget:self action:@selector(nextclick) forControlEvents:UIControlEventTouchUpInside];
  nextStepButton.layer.masksToBounds = YES;
  nextStepButton.layer.cornerRadius = 8;
  [self.view addSubview:nextStepButton];

  
}

- (void)createApplyView {
  
  CGFloat fontF = 13.5f;
  if (IS_IPHONE5) {
    fontF = 11.5f;
  }
  
  UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, _stepHeadView.yt_bottom + 20, _contenView.yt_width - 30, 30)];
  titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f];
  titleLabel.textColor = [UIColor blackColor];
  titleLabel.textAlignment = NSTextAlignmentLeft;
  titleLabel.text = YTLocalizedString(@"填写车辆资料");
  [_contenView addSubview:titleLabel];
  
  UILabel *hanNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.yt_left + 10, titleLabel.yt_bottom + 10, 85, 35)];
  hanNameLabel.textColor = [UIColor blackColor];
  hanNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:fontF];
  hanNameLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"中文姓名")];
  hanNameLabel.textAlignment = NSTextAlignmentLeft;
  [_contenView addSubview:hanNameLabel];

  
}

- (void)cancelclick {
  
}

- (void)nextclick {
  ApplyOilThirdVC *thirdVC = [[ApplyOilThirdVC alloc] init];
  thirdVC.applyModel = self.applyModel;
  [self.navigationController pushViewController:thirdVC animated:YES];
  
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return ScreenWidth * 0.25;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  static NSString *identify = @"OilCradCreateCell";
  OilCradCreateCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
  if (!cell) {
    cell = [[OilCradCreateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
  }
  
  OilCradCreateModel *model = array[indexPath.row];
  model.row = indexPath.row;
  cell.model = model;
  __weak typeof(self) weakSelf = self;
  cell.addCellBlock = ^(BOOL isAdd) {
    [weakSelf addCell:isAdd inRow:indexPath.row];
  };
  return cell;
  
}

- (void)addCell:(BOOL)isAdd inRow:(NSInteger)row {
  [self.view endEditing:YES];
  
  if (isAdd) {
    OilCradCreateModel *model = [[OilCradCreateModel alloc] init];
    model.isAdd = NO;
    [array addObject:model];
    [self.tableView reloadData];
  } else {
    [array removeObjectAtIndex:row];
    [self.tableView reloadData];
  }
  
}


@end
